package com.zking.web.ZC;

import com.zking.dao.domain.Patient;
import com.zking.dao.domain.VerCode;
import com.zking.service.Patient_LoginService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/toregister")
public class Register_Controller {
    String code=null;
    @Autowired
    private Patient_LoginService patientService;
    @RequestMapping("/send")
    public String  SendMsg(String phone,HttpServletRequest request){
      VerCode verCode = new VerCode();
        if(phone==""){phone=null;}
     System.out.println("点击验证码后传过来的电话"+phone);
     if(phone!=null){
         code=verCode.sendCode(phone);
     }else{
         request.setAttribute("loginError", "未填电话");
         return "Front_page/register";
     }
      return  null;
    }

    @RequestMapping (value = "/register", method = RequestMethod.POST)
    public String register(String name, String phone, String password,String verCode, HttpServletRequest request) {
        if(name.equals("")){name = null;}
        if(phone.equals("")){phone = null;}
        if(password.equals("")){password= null;}
        System.out.println("name="+name);
        System.out.println("password="+password);
        System.out.println("phone="+phone);
        Patient patient1 = new Patient();
        //加密
        String hashName ="MD5";
        Object salt = null;
        int hashtime = 999;
        patient1.setName(name);
        patient1.setPhone(phone);
        patient1.setPassword(password);
        //进行前端提交数据的验证，然后存到数据库中
        if (patient1.getName() != null) {
            System.out.println("名字不为空");
            if (patient1.getPhone() != null) {
                System.out.println("电话不为空");
                if (patient1.getPassword() != null) {
                    System.out.println("密码不为空");
                    String a = String.valueOf(new SimpleHash(hashName,password,salt,hashtime));
                    patient1.setPassword(a);
                    System.out.println(a);
                    if(patientService.getPatientByName(patient1)!=null){
                        System.out.println("名字存在");
                        request.setAttribute("loginError", "名字已存在");
                        return "Front_page/register";

                    }else {
                        System.out.println("名字不存在");
                        if(patientService.getPatient(patient1)!=null){
                            request.setAttribute("loginError", "电话已存在");
                            return "Front_page/register";
                        }else {
                            System.out.println("电话不存在");
                            if(verCode.equals(code)){
                                boolean b = patientService.insertPatient(patient1);
                                System.out.println("注册成功"+b);
                                if (b == true) {
                                    request.setAttribute("loginError", "注册成功，请重新登录");
                                    return "Front_page/front_index";
                                }
                                }else{
                                request.setAttribute("loginError", "验证码错误或者为空");
                                return "Front_page/register";
                            }
                        }
                    }

                }else {
                    request.setAttribute("loginError", "密码不能为空");
                    return "Front_page/register";
                }
            } else {
                request.setAttribute("loginError", "电话不能为空");          // 设置错误属性
                //request.getRequestDispatcher("/webapp/WEB-INF/Front_page/judegeIndex.jsp").forward(request, response);
                return "Front_page/register";
            }
        } else {
            System.out.println("姓名不能为空");
            request.setAttribute("loginError", "姓名不能为空");          // 设置错误属性
            return "Front_page/register";
        }
    return  null;
    }

}



